package com.jcgyl.export.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.jcgyl.export.model.Result;
import com.jcgyl.export.model.ResultCode;

@CrossOrigin
@RestControllerAdvice
@ControllerAdvice(annotations = RestController.class)
public class GlobalExceptionHandler {

	private static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	@ExceptionHandler
	public Result processException(Exception ex, HttpServletRequest request, HttpServletResponse response) {
		ex.printStackTrace();
		Result result = new Result();
		String tips = "参数不合法";

		if (ex instanceof MissingServletRequestParameterException) {
			tips = "缺少参数";
			result = new Result(ResultCode.PARAMETER_ERROR);
		} else {
			tips = "发生内部异常";
			result = new Result(ResultCode.SERVER_INTERNAL_ERROR);
		}
		result.setMsg(tips);
		return result;

	}

}
